package org.tloss.web.editor.database.editors;

import org.tloss.web.editor.model.Database;

public class DatabaseConfig {

	public final static String JDBCURL = "JDBC Url";
	public final static String JDBCUSERNAME = "JDBC Username";
	public final static String JDBCPASSWORD = "JDBC Password";
	public final static String DRIVERCLASS = "Driver class";
	public final static String JDBCLIB = "JDBC Lib";
	public final static String ONFFLINE_MODE = "Offline Mode";
	//final static String DATABASE_TYPE = "Database Type";
	//final static String[] DATABASE_SUPPORTED = new String[] { "mysql", "oracle" };

	String name;
	Object value;
	Database database;

	public DatabaseConfig(String name, Database database) {
		this.name = name;
		this.database = database;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Object getValue() {
		if (JDBCLIB.equals(name)) {
			value = database.getJdbcLib();
		} else if (DRIVERCLASS.equals(name)) {
			value = database.getDriverClass();
		} else if (JDBCPASSWORD.equals(name)) {
			value = database.getJdbcPassword();
		} else if (JDBCURL.equals(name)) {
			value = database.getJdbcURL();
		} else if (JDBCUSERNAME.equals(name)) {
			value = database.getJdbcUsername();
		} else if (ONFFLINE_MODE.equals(name)) {
			if (!database.getOffineModde()) {
				value = 0;
			} else {
				value = 1;
			}
		} 
		return value;
	}
	public Object getLabelValue() {
		if (JDBCLIB.equals(name)) {
			value = database.getJdbcLib();
		} else if (DRIVERCLASS.equals(name)) {
			value = database.getDriverClass();
		} else if (JDBCPASSWORD.equals(name)) {
			value = database.getJdbcPassword();
		} else if (JDBCURL.equals(name)) {
			value = database.getJdbcURL();
		} else if (JDBCUSERNAME.equals(name)) {
			value = database.getJdbcUsername();
		} else if (ONFFLINE_MODE.equals(name)) {
			if (!database.getOffineModde()) {
				value = 0;
			} else {
				value = 1;
			}
		}
		return value;
	}

	public void setValue(Object value) {
		if (JDBCLIB.equals(name)) {
			database.setJdbcLib((String) value);
		} else if (DRIVERCLASS.equals(name)) {
			database.setDriverClass((String) value);
		} else if (JDBCPASSWORD.equals(name)) {
			database.setJdbcPassword((String) value);
		} else if (JDBCURL.equals(name)) {
			database.setJdbcURL((String) value);
		} else if (JDBCUSERNAME.equals(name)) {
			database.setJdbcUsername((String) value);
		} else if (ONFFLINE_MODE.equals(name)) {
			database.setOffineModde(Integer.parseInt(value.toString()) > 0);
		}

		this.value = value;
	}

}
